﻿local shader
local screen

local scenery = {}
scenery.coords = {
	-- LS
	{ 2826.9487304688, -1558.3054199219, 41.897464752197, 2766.4284667969, -1603.9888916016, -23.296993255615 }, -- terro spawn
	{ 1232.8095703125, -2061.9184570313, 78.163314819336, 1142.9624023438, -2021.5745849609, 60.847114562988 }, -- pm/bg spawn
	{ 1525.8956298828, -1706.2969970703, 13.868723869324, 1592.9481201172, -1632.6173095703, 22.544437408447 }, -- cop spawn
	{ 1093.7541503906, -1540.4106445313, 48.800392150879, 1129.8587646484, -1456.4135742188, 8.2916679382324 }, -- mall
	{ 1227.2884521484, -828.07489013672, 92.638542175293, 1300.1320800781, -760.18005371094, 83.467834472656 }, -- mansion
	{ 1530.4390869141, -732.03814697266, 95.980361938477, 1469.3288574219, -653.15344238281, 102.51789093018 }, -- ylw mansion
	{ 1973.9248046875, -1435.1187744141, 28.117031097412, 1898.6204833984, -1375.6237792969, 0.017177581787109 }, -- skatepark
	{ 1488.0001220703, -1356.6197509766, 352.83831787109, 1567.5255126953, -1320.2835693359, 304.30596923828 }, -- tall bldng
	{ 346.6794128418, -1999.4156494141, 45.192832946777, 392.90072631836, -2061.7980957031, -17.831031799316 }, -- wheel
	{ 1665.1136474609, -2269.0732421875, 80.767822265625, 1569.8051757813, -2278.1391601563, 51.887084960938 }, -- airport
	-- SF
	{ -2032.2498779297, 1446.3288574219, 14.193745613098, -2106.1176757813, 1378.9445800781, 12.485026359558 }, -- terro spawn
	{ -2304.6496582031, 1501.8555908203, 35.189075469971, -2389.6020507813, 1552.3641357422, 19.95841217041 }, -- bc
	{ -1681.1265869141, 630.27679443359, 92.236709594727, -1637.7976074219, 695.24163818359, 29.769386291504 }, -- cop spawn
	{ -2770.6379394531, -189.2042388916, 32.353382110596, -2718.3068847656, -272.84698486328, 16.063835144043 }, -- pm/bg spawn
	{ -2443.6267089844, -581.06018066406, 135.41038513184, -2511.6838378906, -647.75622558594, 165.73991394043 }, -- teletower
	{ -1680.6584472656, -1.0051213502884, 3.8720488548279, -1597.8001708984, 53.55587387085, 16.426023483276 }, -- cargo ship
	{ -1511.1510009766, 1371.0656738281, 3.7969086170197, -1446.0040283203, 1446.9050292969, 1.730667591095 }, -- sc
	{ -2630.3706054688, 978.45642089844, 99.52254486084, -2698.5390625, 924.26489257813, 50.366386413574 }, -- rich houses
	{ -2005.0399169922, 129.8581237793, 34.015148162842, -2082.2805175781, 188.66632080078, 10.025947570801 }, -- cj garage
	{ -1257.90234375, 61.36665725708, 67.458564758301, -1318.3825683594, -17.778125762939, 58.613067626953 }, -- airport
	-- LV
	{ 2137.4343261719, 1880.2232666016, 13.005018234253, 2048.0461425781, 1914.1412353516, 42.319271087646 }, -- visage
	{ 2216.8640136719, 2385.3283691406, 24.493120193481, 2286.3334960938, 2455.3901367188, 8.2034740447998 }, -- cop spawn
	{ 2745.3125, 957.34515380859, 19.096334457397, 2818.9787597656, 891.98162841797, 1.7497501373291 }, -- terro spawn
	{ 2052.0302734375, 986.94366455078, 13.865134239197, 2004.3891601563, 1074.4888916016, 5.7318630218506 }, -- 4 dragons
	{ 1783.3311767578, 1454.1156005859, 27.972105026245, 1688.7934570313, 1443.6185302734, -2.8892841339111 }, -- pm/bg spawn
	{ 2070.146484375, 1718.7390136719, 28.895292282104, 2147.9289550781, 1663.7996826172, -1.6259021759033 }, -- caligulas
	{ 2590.0693359375, 2229.7419433594, 37.345249176025, 2648.3327636719, 2307.7084960938, 14.398359298706 }, -- vrock
	{ 2256.4296875, 2146.6743164063, 12.530492782593, 2180.4086914063, 2209.8791503906, 27.562389373779 }, -- bannerlights
	{ 2075.8012695313, 1246.3132324219, 23.226152420044, 2157.6120605469, 1302.3059082031, 10.118136405945 }, -- sfinx/pyramid
	{ 1696.7142333984, 2003.1180419922, 29.537368774414, 1642.0747070313, 2083.2570800781, 5.2014255523682 }, -- random houses
	-- MISC
	{ 2677.9406738281, 2792.84375, 38.792037963867, 2604.8078613281, 2727.0456542969, 20.84211730957 }, -- factory
	{ 182.0728302002, 2544.0744628906, 33.445293426514, 274.69543457031, 2568.7805175781, 4.9731140136719 }, -- airstrip
	{ 261.88311767578, 1848.5401611328, 14.431158065796, 216.98382568359, 1931.7517089844, -18.124937057495 }, -- area51
	{ -438.73645019531, 1657.1767578125, 119.6722869873, -340.42599487305, 1639.7619628906, 114.03494262695 }, -- satellite
	{ -2364.3278808594, 2186.1879882813, 9.9250144958496, -2446.4123535156, 2243.2868652344, 8.5735864639282 }, -- bayside
	{ -2220.8718261719, -1751.0079345703, 486.88412475586, -2292.6525878906, -1682.0930175781, 476.97125244141 }, -- chiliad
	{ -1474.9484863281, -1458.587890625, 130.76174926758, -1415.392578125, -1525.5209960938, 86.342712402344 }, -- farm
	{ -1017.9432373047, -706.61224365234, 85.46614074707, -1076.0217285156, -632.7578125, 51.225593566895 }, -- power plant
	{ 1245.5681152344, 394.53137207031, 32.068786621094, 1304.1330566406, 315.51019287109, 14.018672943115 } -- village
}

function startLoginScenery() 
	addEventHandler( "onClientRender", root, drawBars )
	addEventHandler( "onClientResourceStart", root, checkBlackScreen )
	
	enableGrayness()
	fadeCamera( true )
	showChat( false )
	showPlayerHudComponent( "all", false )
	
	changeScenery()
	scenery.timer = setTimer( changeScenery, 7000, 0 )
end

function changeScenery()
	setCameraMatrix( unpack( scenery.coords[math.random( #scenery.coords )] ) )
end

function stopLoginScenery()
	removeEventHandler( "onClientRender", root, drawBars )
	removeEventHandler( "onClientResourceStart", root, checkBlackScreen )
	
	disableGrayness()
	showChat( true )
	showPlayerHudComponent( "all", true )
	
	killTimer( scenery.timer )
	scenery.timer = nil
end

function drawBars()
	local height = 0.15
	dxDrawRectangle( 0, 0, screenX, screenY*height, tocolor( 0, 0, 0, 255 ) )
	dxDrawRectangle( 0, screenY-screenY*height, screenX, screenY*height, tocolor( 0, 0, 0, 255 ) )
end

-- camera fades to black every map change, so this reverts it for a local player
-- who is in the login screen
function checkBlackScreen( resource )
	local name = getResourceName( resource )
	local found = string.find( name, "ptpm" )
	if found == 1 then -- starts with "ptpm"
		changeScenery()
		
		fadeCamera( true, 0 )
		showChat( false )
		showPlayerHudComponent( "all", false )
	end
end

-- GRAY SHADER
function enableGrayness()
	shader = dxCreateShader ( "resources/grey.fx" )
	screen = dxCreateScreenSource ( screenX, screenY )
	
	if not shader or not screen then
		return
	end
	
	dxSetShaderValue( shader, "gTexture", screen )
	addEventHandler( "onClientHUDRender", root, renderBackground )
	
	local shaderPrimary = 0.1
	local shaderSecondary = 0.1
	dxSetShaderValue( shader, "gColorPrimary", shaderPrimary )
	dxSetShaderValue( shader, "gColorSecondary", shaderSecondary )
end

function disableGrayness ()
	if isElement( shader ) then destroyElement( shader ) end
	if isElement( screen ) then destroyElement( screen ) end
	removeEventHandler( "onClientHUDRender", root, renderBackground )
end

function renderBackground()
	dxSetRenderTarget()
	dxUpdateScreenSource ( screen )
	dxDrawImage ( 0, 0, screenX, screenY, shader )
end